package com.grgbanking.encryp.netty.config;

import com.grgbanking.encryp.netty.server.NettyServer;
import com.grgbanking.encryp.netty.server.handler.TcpServerHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class NettyServerConfig {

    @Autowired
    private TcpServerHandler tcpServerHandler;
    @Value("${netty.port}")
    private int port;

    @Bean
    public NettyServer nettyServer() {
        return new NettyServer(port, tcpServerHandler);
    }

    //当有2个及以上的 Runnable的@Bean方法时，请并指定名称，然后通过名称获取Bean
    @Bean(name = "nettyServerRunner")
    public Runnable nettyServerRunner(NettyServer nettyServer) {
        return () -> {
            try {
                nettyServer.run();
            } catch (Exception e) {
                e.printStackTrace();
            }
        };
    }

}
